//
// Created by ta&to on 2022/10/28.
//


#include "http_net_config.h"

static int _clear_req_head(struct requestContext *request) {
    dtsfree(request->home);
    dtsfree(request->head);
    dtsfree(request->reqType);
    dtsfree(request->reqPath);
    dtsfree(request->version);
    return TRUE;
}

static struct requestContext *_make_req_head(dts headstr) {
    char method[12] = { 0 };
    char path[PATH_MAX] = { 0 };
    char version[1024] = { 0 };
    struct requestContext *request = (struct requestContext *) http_malloc(sizeof(struct requestContext));
    sscanf(headstr, "%[^ ] %[^ ] %[^\r\n]", method, path, version);
    request->home = dtsnew("/opt/");
    request->head = headstr;
    request->reqType = dtsnew(method);
    request->reqPath = dtsnew(path);
    request->version = dtsnew(version);
    return request;
}
/**
 * http 的请求头处理
 */
void http_request_header_handler(req_header_t * header) {
    mutex_lock(&header->hset->hgdc->req_maps_header_lock);
    noder headnode = RBtreenodesel(&header->hset->hgdc->req_maps_header, header->fds->fdc);
    if(ISNULL(headnode)){
        mutex_unlock(&header->hset->hgdc->req_maps_header_lock);
        return;
    }
    dts headstr = (dts)headnode->spot->value;
    dts headstrclone = dtsdup(headstr);
    mutex_unlock(&header->hset->hgdc->req_maps_header_lock);

    struct requestContext *request = _make_req_head(headstrclone);
    request->rh = header;
    request->fd = header->fd;
    request->fds = header->fds;
    request->home = dtsnew("/");
#if defined(FILE_SYSTEM_EVENT)
    file_system_app(request);
#endif
    debug(header->hset->logs, "%s[%d]", "http请求处理完成!!", header->fd);
    _clear_req_head(request);
}